﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using geoCRM_Services.DataAccess;

namespace geoCRM_Services.TestDataAccess
{
    /// <summary>
    /// Summary description for TestContactDataAccess
    /// </summary>
    [TestClass]
    public class TestContactDataAccess
    {
        
        public TestContactDataAccess()
        {
           
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
         //Use ClassInitialize to run code before running the first test in the class
         //[ClassInitialize()]
         //public static void MyClassInitialize(TestContext testContext) 
         //{
        
         //}
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestGetContacts_DAL_ShouldReturnContactsBusinessEntitiesPaged()
        {
            int pageindex, pagesize;
            pageindex = 0;
            pagesize = 10;
            geoCRM_Services.DataAccess.IContactDataAccess contactDAL = geoCRM_Services.DataAccess.ContactDAL.GetImplementor();
            IEnumerable<geoCRM_Services.BusinessEntities.Contact> contacts = contactDAL.GetContacts(pageindex, pagesize);
            Assert.IsNotNull(contacts);
            Assert.IsTrue(contacts.Count() <= pagesize);
        }
        [TestMethod]
        public void TestGetContactsCount_DAL_ShouldReturnTheTotalNumberOfContactsInTheRepository()
        {
            geoCRM_Services.DataAccess.IContactDataAccess contactDAL = geoCRM_Services.DataAccess.ContactDAL.GetImplementor();
            int contactscount = contactDAL.GetContactsCount();
            Assert.IsTrue(contactscount > 0);
        }
        [TestMethod]
        public void TestGetContactByID_DAL_ShouldReturnASpecificContactByID()
        {
            geoCRM_Services.DataAccess.ContactDBDataContext db = new geoCRM_Services.DataAccess.ContactDBDataContext();
            int SearchedID = GetExistingIDFromDB(db);

            geoCRM_Services.DataAccess.IContactDataAccess contactDAL = geoCRM_Services.DataAccess.ContactDAL.GetImplementor();
            
            geoCRM_Services.BusinessEntities.Contact contact = contactDAL.GetContactByID(SearchedID);
            Assert.IsTrue(contact.ID == SearchedID);
        }
        [TestMethod]
        public void TestGetContactByID_WithAddresses_DAL_ShouldReturnASpecificContactByIDWithItsAddresses()
        {
            geoCRM_Services.DataAccess.ContactDBDataContext db = new geoCRM_Services.DataAccess.ContactDBDataContext();
            int SearchedID = GetExistingID_FromDB_ContactWithAddresses(db);
           
            geoCRM_Services.DataAccess.IContactDataAccess contactDAL = geoCRM_Services.DataAccess.ContactDAL.GetImplementor();

            geoCRM_Services.BusinessEntities.Contact contact = contactDAL.GetContactByID(SearchedID);
            Assert.IsTrue(contact.ID == SearchedID);
            Assert.IsTrue(contact.Contact_Addresses.Count > 0);
        }

        


        #region Helper Methods
        private static int GetExistingIDFromDB(geoCRM_Services.DataAccess.ContactDBDataContext db)
        {
            using (db)
            {
                int SearchedID = (from c in db.Contacts
                                  select c.ID).First();
                return SearchedID;
            }

        }
        private static int GetExistingID_FromDB_ContactWithAddresses(geoCRM_Services.DataAccess.ContactDBDataContext db)
        {
            int SearchedID = (from c in db.Contacts
                          where c.Contact_Addresses.Count > 0
                          select c.ID).First();
            return SearchedID;
        }
        #endregion
    }
}
